/*
 * Created by JFormDesigner on Sun Jun 20 08:41:31 GMT+08:00 2021
 */

package com.bk.view;

import java.awt.event.*;
import com.bk.dao.UserDao;
import com.bk.model.User;
import com.bk.util.ToolUtil;

import java.awt.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author Ayi
 */
public class blackList extends JFrame {
    public blackList() {
        initComponents();
        try {
            set();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    // 初始化黑名单
    public void set() throws SQLException, ClassNotFoundException {
        // 重定义模型
        Vector<Vector<String>> vtUser = new Vector<Vector<String>>();
        Vector<String> headUser = new Vector<String>();
        headUser.add("用户id");
        headUser.add("用户名");
        headUser.add("借阅量");
        headUser.add("信用");
        DefaultTableModel userTableModel = new DefaultTableModel(vtUser, headUser);
        blackListUser.setModel(userTableModel);
        // 从数据库取表
        ResultSet ut = new UserDao().getblackListTable();
        // 初始化UI表内数据
        while (ut.next()) {
            Vector<String> temp = new Vector<String>();
            temp.add(ut.getString("blid"));
            temp.add(ut.getString("blname"));
            temp.add(ut.getString("blbooknums"));
            temp.add(ut.getString("blcredit"));
            vtUser.add(temp);
        }
        scrollPane0.updateUI();
    }

    // 刷新，监听
    private void blButtonFreshActionPerformed(ActionEvent e) {
        // TODO add your code here
        try {
            set();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException classNotFoundException) {
            classNotFoundException.printStackTrace();
        }
    }

    // 加入黑名单，监听
    private void blButtonAddActionPerformed(ActionEvent e) {
        // TODO add your code here
        // 查找
        String idOrNameInfo = JOptionPane.showInputDialog(null, "告诉我你要把谁加入小黑屋（ id 或用户名）", "加入黑名单", 1);
        User user = null;
        if (idOrNameInfo != null) {
            boolean bool = new ToolUtil().judgeIdOrName(idOrNameInfo);
            if (bool) {
                try {
                    user = new UserDao().searchUserById(idOrNameInfo);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }else{
                try {
                    user = new UserDao().searchUserByName(idOrNameInfo);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (user == null) {
                JOptionPane.showMessageDialog(null, "这个用户不存在！");
                return;
            }
        }else {
            return;
        }
        if (user.getUname().equals("ayi")) {
            JOptionPane.showMessageDialog(null, "超级管理员不可以加入黑名单！");
            return;
        }
        // 转数据库操作，加入黑名单
        try {
            new UserDao().addblackListTable(user);
            JOptionPane.showMessageDialog(null, "用户 " + user.getUname() + "成功加入黑名单！");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        blButtonFresh.doClick();
    }

    // 移除黑名单，监听
    private void blbuttonOutActionPerformed(ActionEvent e) {
        // TODO add your code here
        // 查找
        String idOrNameInfo = JOptionPane.showInputDialog(null, "告诉我你要把谁放出去（ id 或用户名）", "移出黑名单", 1);
        User user = null;
        if (idOrNameInfo != null) {
            boolean bool = new ToolUtil().judgeIdOrName(idOrNameInfo);
            if (bool) {
                try {
                    user = new UserDao().searchBlUserById(idOrNameInfo);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }else{
                try {
                    user = new UserDao().searchBlUserByName(idOrNameInfo);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (user == null) {
                JOptionPane.showMessageDialog(null, "这个用户不存在！");
                return;
            }
        }else {
            return;
        }
        // 转数据库操作，移出黑名单
        try {
            new UserDao().outblackListTable(user);
            JOptionPane.showMessageDialog(null, "成功移出！");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        blButtonFresh.doClick();
    }

    // 返回，监听
    private void blButtonBackActionPerformed(ActionEvent e) {
        // TODO add your code here
        dispose();
    }

    // 永久删除，监听
    private void blButtonForeverOutActionPerformed(ActionEvent e) {
        // TODO add your code here
        // 查找
        String idOrNameInfo = JOptionPane.showInputDialog(null, "要彻底删除谁呢（ id 或用户名）", "永久删除", 1);
        User user = null;
        if (idOrNameInfo != null) {
            boolean bool = new ToolUtil().judgeIdOrName(idOrNameInfo);
            if (bool) {
                try {
                    user = new UserDao().searchBlUserById(idOrNameInfo);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }else{
                try {
                    user = new UserDao().searchBlUserByName(idOrNameInfo);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (user == null) {
                JOptionPane.showMessageDialog(null, "这个用户不存在！");
                return;
            }
        }else {
            return;
        }
        // 转数据库操作，永久删除
        try {
            new UserDao().foreverOutblackListTable(user);
            JOptionPane.showMessageDialog(null, "该用户灰飞烟灭！");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        blButtonFresh.doClick();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        scrollPane0 = new JScrollPane();
        blackListUser = new JTable();
        blButtonAdd = new JButton();
        blbuttonOut = new JButton();
        blButtonForeverOut = new JButton();
        blButtonBack = new JButton();
        blButtonFresh = new JButton();

        //======== this ========
        setTitle("\u9ed1\u540d\u5355");
        setResizable(false);
        Container contentPane = getContentPane();

        //======== scrollPane0 ========
        {

            //---- blackListUser ----
            blackListUser.setPreferredSize(new Dimension(150, 40));
            blackListUser.setModel(new DefaultTableModel(
                new Object[][] {
                    {"", "", null, "", null},
                },
                new String[] {
                    "\u7528\u6237id", "\u7528\u6237\u540d", "\u501f\u9605\u91cf", "\u4fe1\u7528", " "
                }
            ) {
                Class<?>[] columnTypes = new Class<?>[] {
                    Object.class, String.class, Integer.class, String.class, Object.class
                };
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return columnTypes[columnIndex];
                }
            });
            {
                TableColumnModel cm = blackListUser.getColumnModel();
                cm.getColumn(4).setCellEditor(new DefaultCellEditor(
                    new JComboBox(new DefaultComboBoxModel(new String[] {
                        "1"
                    }))));
            }
            blackListUser.setFillsViewportHeight(true);
            blackListUser.setEnabled(false);
            blackListUser.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));
            scrollPane0.setViewportView(blackListUser);
        }

        //---- blButtonAdd ----
        blButtonAdd.setText("\u52a0\u5165\u9ed1\u540d\u5355");
        blButtonAdd.setBackground(Color.white);
        blButtonAdd.addActionListener(e -> blButtonAddActionPerformed(e));

        //---- blbuttonOut ----
        blbuttonOut.setText("\u79fb\u51fa\u9ed1\u540d\u5355");
        blbuttonOut.setBackground(Color.white);
        blbuttonOut.addActionListener(e -> blbuttonOutActionPerformed(e));

        //---- blButtonForeverOut ----
        blButtonForeverOut.setText("\u6c38\u4e45\u5220\u9664");
        blButtonForeverOut.setBackground(Color.white);
        blButtonForeverOut.addActionListener(e -> blButtonForeverOutActionPerformed(e));

        //---- blButtonBack ----
        blButtonBack.setText("\u8fd4\u56de");
        blButtonBack.setBackground(Color.white);
        blButtonBack.addActionListener(e -> blButtonBackActionPerformed(e));

        //---- blButtonFresh ----
        blButtonFresh.setText("\u5237\u65b0");
        blButtonFresh.setBackground(Color.white);
        blButtonFresh.addActionListener(e -> blButtonFreshActionPerformed(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addComponent(scrollPane0, GroupLayout.PREFERRED_SIZE, 485, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                        .addComponent(blButtonFresh, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(blButtonForeverOut, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(blbuttonOut, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(blButtonAdd, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(blButtonBack, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap(8, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addComponent(scrollPane0, GroupLayout.PREFERRED_SIZE, 368, GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE))
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(blButtonAdd)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(blbuttonOut)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(blButtonForeverOut)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(blButtonFresh)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(blButtonBack)
                    .addGap(15, 15, 15))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JScrollPane scrollPane0;
    private JTable blackListUser;
    private JButton blButtonAdd;
    private JButton blbuttonOut;
    private JButton blButtonForeverOut;
    private JButton blButtonBack;
    private JButton blButtonFresh;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
